/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yangxu.hospital.system.modules.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yangxu.hospital.common.utils.FileUtil;
import com.yangxu.hospital.common.utils.PageResult;
import com.yangxu.hospital.common.utils.PageUtil;
import com.yangxu.hospital.system.modules.system.domain.SysDepartment;
import com.yangxu.hospital.system.modules.system.domain.SysDoctor;
import com.yangxu.hospital.system.modules.system.domain.dto.DoctorDto;
import com.yangxu.hospital.system.modules.system.service.SysDepartmentService;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangxu.hospital.system.modules.system.service.SysDoctorService;
import com.yangxu.hospital.system.modules.system.domain.vo.SysDoctorQueryCriteria;
import com.yangxu.hospital.system.modules.system.mapper.SysDoctorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @description 服务实现
* @author 杨旭
* @date 2024-11-25
**/
@Service
@RequiredArgsConstructor
public class SysDoctorServiceImpl extends ServiceImpl<SysDoctorMapper, SysDoctor> implements SysDoctorService {

    private final SysDoctorMapper sysDoctorMapper;
    private final SysDepartmentService sysDepartmentService;

    /**
     * 查询医生信息
     */
    @Override
    public PageResult<DoctorDto> queryAll(SysDoctorQueryCriteria criteria, Page<Object> page) {
        IPage<DoctorDto> all = sysDoctorMapper.findAllDoctor(criteria, page);
        List<SysDepartment> departments = sysDepartmentService.list();

        // 创建一个 Map 来快速查找部门名称
        Map<Long, String> departmentMap = new HashMap<>();
        for (SysDepartment department : departments) {
            departmentMap.put(department.getDepartmentId(), department.getName()); // 确保使用正确的字段名
        }
        // 遍历 all 中的数据，设置科室名称
        for (DoctorDto doctorDto : all.getRecords()) {
            if (doctorDto.getDepartmentName() == null && doctorDto.getDepartmentId() != null) {
                Long departmentId = doctorDto.getDepartmentId(); // 确保这里获取的是 departmentId
                String departmentName = departmentMap.get(departmentId);
                if (departmentName != null) {
                    doctorDto.setDepartmentName(departmentName);
                } else {
                    // 如果 departmentId 在 departmentMap 中找不到对应的 departmentName，可以设置一个默认值或空字符串
                    doctorDto.setDepartmentName("未分配科室"); // 或者你可以设置为 "未知"
                }
            }
        }
        return PageUtil.toPage(all);
    }

    @Override
    public List<SysDoctor> queryAll(SysDoctorQueryCriteria criteria){
        return sysDoctorMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SysDoctor resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDoctor resources) {
        SysDoctor sysDoctor = getById(resources.getDoctorId());
        sysDoctor.copy(resources);
        saveOrUpdate(sysDoctor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<SysDoctor> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysDoctor sysDoctor : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("科室ID", sysDoctor.getDepartmentId());
            map.put("职称", sysDoctor.getProfessionalTitle());
            map.put("专长领域", sysDoctor.getSpecialty());
            map.put("创建时间", sysDoctor.getCreateTime());
            map.put("更新时间", sysDoctor.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<DoctorDto> getAllDoctor() {
        List<DoctorDto> allDoctor = sysDoctorMapper.getAllDoctor();
        return allDoctor;
    }

    @Override
    public List<DoctorDto> toDaylist(String fullDate,String departmentId) {
        return sysDoctorMapper.toDayList( fullDate, departmentId);
    }
}